package net.sahv.bdyz.controller.web;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import net.sahv.bdyz.model.Fabric;
import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.model.Pattern;
import net.sahv.bdyz.model.PatternPrint;
import net.sahv.bdyz.model.ShoppingCart;
import net.sahv.bdyz.service.FabricService;
import net.sahv.bdyz.service.MemberService;
import net.sahv.bdyz.service.PatternPrintService;
import net.sahv.bdyz.service.PatternService;
import net.sahv.bdyz.service.ShoppingCartService;
import net.sahv.bdyz.util.Jiage;
import net.sahv.bdyz.util.LoadCfgUtil;
import net.sahv.bdyz.util.PageInfo;

@Controller
@RequestMapping("web/shoppingcart/")
public class ShoppingCartController {
	@Autowired
	private ShoppingCartService shoppingCartService;
	@Autowired
	private PatternService patternService;
	@Autowired
	private FabricService fabricService;
	@Autowired
	private PatternPrintService patternPrintService;

	@Autowired
	private MemberService memberService;
	@Autowired 
	CookieLocaleResolver resolver;
	
	/**
	 * 加入购物车
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param request
	 * @param status
	 *            分类 0、印花；1、买断；2、成品；3、面料；
	 * @param pId
	 *            花型id
	 * @param ppId
	 *            印花工艺id
	 * @param fId
	 *            面料id
	 * @param size
	 *            数量
	 * @param colorImg
	 *            面料颜色
	 * @return
	 */
	/**
	 * 修改颜色判断，不同颜色为不同数据
	 */
	@ResponseBody
	@RequestMapping(value = "insertshoppingcart", method = RequestMethod.POST)
	public ApiResult insertshoppingcart(HttpServletRequest request, String status, String pId, String ppId, String fId,
			String size, String colorImg) {
		Integer sizee = Integer.valueOf(size);
		ApiResult result = new ApiResult();
		ShoppingCart shoppingCart = new ShoppingCart();
		Member member = memberService.getCurrent();//获取当前登录人信息
		if(member==null){
			result.msg.status = 3;//
			result.msg.desc = "未登录";
			return result;
		}
		if (status.equals("0")) {//50米以下2倍     51~300米1.5倍    301~1000米1.2倍   1000米以上1倍
			PatternPrint patternPrint = patternPrintService.selectByPrimaryKey(Integer.valueOf(ppId));// 印花工艺
			Pattern pattern = patternService.selectByPrimaryKey(Integer.valueOf(pId));// 花型
			if (pattern.getpBuyoutState().equals(1)) {
				result.msg.status = 1;//
				result.msg.desc = "该花型已被买断，操作失败";
				return result;
			}
			ShoppingCart shoppingCart1 = shoppingCartService.selectstatusling(pattern.getpNo(), member.getmId(),patternPrint.getPpId());
			if (shoppingCart1 != null) {
				sizee=shoppingCart1.getScFabricSize() + sizee;
				shoppingCartService.deleteByPrimaryKey(shoppingCart1.getScId());
			}
			shoppingCart.setScPatternNo(pattern.getpNo());
			shoppingCart.setScPatternImg(pattern.getpImg());
			shoppingCart.setScPatternSize(pattern.getChicun());
			shoppingCart.setScPatternSinglePrice(patternPrint.getPpPrice());
			shoppingCart.setScFabricSize(Integer.valueOf(sizee));
			shoppingCart.setScCreateTime(new Date());
			shoppingCart.setmId(member.getmId());
			shoppingCart.setPpId(patternPrint.getPpId());
			shoppingCart.setScType(0);
			BigDecimal heji = Jiage.yinhua(sizee);
			
			shoppingCart.setScTotalPrice(patternPrint.getPpPrice().multiply(heji));
		
			
			shoppingCartService.insert(shoppingCart);
			return result;
		}
		if (status.equals("1")) {
			Pattern pattern = patternService.selectByPrimaryKey(Integer.valueOf(pId));// 花型
			if (pattern.getpBuyoutState().equals(1)) {
				result.msg.status = 1;//
				result.msg.desc = "该花型已被买断，操作失败";
				return result;
			}
			ShoppingCart shoppingCart1 = shoppingCartService.selectstatusyi(pattern.getpNo(), member.getmId());
			if (shoppingCart1 != null) {
				
				return result;
			}

			shoppingCart.setScPatternNo(pattern.getpNo());
			shoppingCart.setScPatternImg(pattern.getpImg());
			shoppingCart.setScPatternSize(pattern.getChicun());
			shoppingCart.setScPatternSinglePrice(pattern.getpBuyoutPrice());
			shoppingCart.setScTotalPrice(pattern.getpBuyoutPrice());
			shoppingCart.setScCreateTime(new Date());
			shoppingCart.setScFabricSize(Integer.valueOf(sizee));
			shoppingCart.setScType(1);
			shoppingCart.setmId(member.getmId());
			shoppingCartService.insert(shoppingCart);
			return result;
		}
		if (status.equals("2")) {
			PatternPrint patternPrint = patternPrintService.selectByPrimaryKey(Integer.valueOf(ppId));// 印花工艺
			Pattern pattern = patternService.selectByPrimaryKey(Integer.valueOf(pId));// 花型
			if (pattern.getpBuyoutState().equals(1)) {
				result.msg.status = 1;//
				result.msg.desc = "该花型已被买断，操作失败";
				return result;
			}
			Fabric fabric = fabricService.selectByPrimaryKey(Integer.valueOf(fId)); // 面料
			ShoppingCart shoppingCart1 = shoppingCartService.selectstatuser(pattern.getpNo(), fabric.getfNo(),
					member.getmId(),patternPrint.getPpId());
			if (shoppingCart1 != null) {
				sizee=shoppingCart1.getScFabricSize() + sizee;
				shoppingCartService.deleteByPrimaryKey(shoppingCart1.getScId());
			}
			shoppingCart.setScPatternNo(pattern.getpNo());
			shoppingCart.setScPatternImg(pattern.getpImg());
			shoppingCart.setScPatternSize(pattern.getChicun());
			shoppingCart.setScPatternSinglePrice(fabric.getfPrice().add(patternPrint.getPpPrice())); // 单价相加

			shoppingCart.setScType(2);

			shoppingCart.setScFabricNo(fabric.getfNo());
			shoppingCart.setScFabricImg(fabric.getfImg());
			shoppingCart.setScFabricSize(Integer.valueOf(sizee));
			shoppingCart.setPpId(patternPrint.getPpId());
			shoppingCart.setScCreateTime(new Date());
			shoppingCart.setmId(member.getmId());
		
			BigDecimal mianliao=new BigDecimal(0); 
			mianliao = fabric.getfPrice().multiply(Jiage.mianliao(sizee));
			
			BigDecimal yinhua=new BigDecimal(0); 
			yinhua = patternPrint.getPpPrice().multiply(Jiage.yinhua(sizee));
			shoppingCart.setScTotalPrice(yinhua.add(mianliao));
			shoppingCartService.insert(shoppingCart);
			
			return result;
		}
		if (status.equals("3")) {
			Fabric fabric = fabricService.selectByPrimaryKey(Integer.valueOf(fId)); // 面料
			ShoppingCart shoppingCart1 = shoppingCartService.selectstatussan(fabric.getfNo(), member.getmId(),colorImg);
			if (shoppingCart1 != null) {
				sizee=shoppingCart1.getScFabricSize() + sizee;
				shoppingCartService.deleteByPrimaryKey(shoppingCart1.getScId());
			}
			shoppingCart.setScType(3);
			shoppingCart.setScFabricNo(fabric.getfNo());
			shoppingCart.setScFabricImg(fabric.getfImg());
			shoppingCart.setScFabricSize(Integer.valueOf(sizee));
			shoppingCart.setScPatternSinglePrice(fabric.getfPrice());
			shoppingCart.setScCreateTime(new Date());
			shoppingCart.setmId(member.getmId());
			BigDecimal heji = Jiage.mianliao(sizee);
			shoppingCart.setFabricColor(colorImg);
			shoppingCart.setScTotalPrice(fabric.getfPrice().multiply(heji));
			shoppingCartService.insert(shoppingCart);
			return result;
		}
		result.msg.status = 2;//
		result.msg.desc = "操作失败";
		return result;

	}

	/**
	 * 购物车移除物品
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param request
	 * @param scId
	 *            购物车id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "delshoppingcart", method = RequestMethod.POST)
	public ApiResult delshoppingcart(HttpServletRequest request, String scId) {
		ApiResult result = new ApiResult();
		Member member = memberService.getCurrent();//获取当前登录人信息
		if (member == null) {
			result.msg.status = 2;
			result.msg.desc = "未登录！请登录";
			return result;
		}
		shoppingCartService.deleteByPrimaryKey(Integer.valueOf(scId));
		return result;

	}

	/**
	 * 查看购物车（查询所有）
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param request
	 * @param pageInfo
	 * @return
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	@RequestMapping(value = "viewshoppingcart", method = RequestMethod.GET)
	public String viewshoppingcart(HttpServletRequest request, PageInfo pageInfo, Model model,HttpServletResponse response) throws FileNotFoundException, IOException {
		// Member member = (Member) request.getSession().getAttribute("member");
		
		Map<String,String> mapUrls=LoadCfgUtil.loadCfg();
		String imgParent=mapUrls.get("targetUrl.img");
		
		Object languageObj=request.getSession().getAttribute("language");
		 String yuyan="";
			if(languageObj!=null){
				String language=String.valueOf(languageObj);
				if(language!=null&&!language.equals("")){
					language=language.toLowerCase();
					if(language.equals("zh_cn")){
						yuyan="ch";
						resolver.setLocale(request, response, Locale.CHINA );
					}else if(language.equals("en")){
						yuyan="en";
						resolver.setLocale(request, response, Locale.ENGLISH );
					}else{
						yuyan="ch";
						resolver.setLocale(request, response, Locale.CHINA );
					}
				}
			}else{
				yuyan="ch";
			}
		Member member = memberService.getCurrent();//获取当前登录人信息
		if (member != null) {
			List<Map<String,Object>> list = shoppingCartService.selectAllByMid(member.getmId(),yuyan);
			for(Map<String,Object> listOne:list){
				String SC_PATTERN_IMG=(String) listOne.get("SC_PATTERN_IMG");
				listOne.remove("SC_PATTERN_IMG");
				listOne.put("SC_PATTERN_IMG", imgParent+SC_PATTERN_IMG);
			}
			//品图片路径
			model.addAttribute("list", list);
		} else {
			model.addAttribute("list", "");
		}
		return "member/member/shoppingCart";

	}

}
